using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Diagnostics;
using System.IO;

namespace Xtreme_Server
{
    public partial class MainWindowEx : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public MainWindowEx()
        {
            InitializeComponent();

            this.Refresh();

            Program.ws.LogSent += this.LogReceivedThread;
            Program.ftp.LogSent += this.LogReceivedThread;
        }



        string l;
        public void LogReceived(object sender, string log)
        {
            l = log;
            //StreamWriter wr = new StreamWriter(Path.Combine(Application.StartupPath, @"conf\tmp.dat"));
            //wr.Write(l);
            //wr.Close();

            //MessageBox.Show(l);
            //if (txLog.Text == "")
            //{
            // Thread.CurrentThread = new Thread(new ThreadStart(LogReceived2));
            //}
            txtLog.Text += l;
            //prt.txtLog.Text += l;

            //txLog.Text += File.ReadAllText(Path.Combine(Application.StartupPath, @"conf\tmp.dat"));
            txtLog.Text += Environment.NewLine;
            //prt.txtLog.Text += Environment.NewLine;
        }

        public void LogReceivedThread(object sender, string s)
        {
            Invoke(new WebServer.LogSentEventHandler(LogReceived), new object[] { this, s });
        }




        private void openWWWRootDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", Program.WWWRoot);
        }

        private void openPHPiniFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(Path.Combine(Program.PHPDir, "php.ini"));
        }

        private void openMYiniFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(Path.Combine(Program.MySQLDir, "my.ini"));
        }

        private void showStandardWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            prt.Location = this.Location;
            prt.ShowInTaskbar = true;
            prt.prt = this;
            this.Hide();
            prt.Show();
            this.ShowInTaskbar = false;
            this.Visible = false;
        }

        private void MainWindowEx_Load(object sender, EventArgs e)
        {
            ntPort.Value = Program.ws.Port;
            ntMaxClients.Value = Program.ws.MaxClient;

            if (Program.ws.Started)
            {
                startToolStripMenuItem.Enabled = false;
                stopToolStripMenuItem.Enabled = true;
                ntPort.Enabled = false;
            }
            else
            {
                startToolStripMenuItem.Enabled = true;
                stopToolStripMenuItem.Enabled = false;
                ntPort.Enabled = true;
            }


        }

        public MainWindow prt { get; set; }

        private void kryptonNumericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            if(CurrentSrv == "HTTP")
                Program.ws.Port = int.Parse(ntPort.Value + "");
            else
                Program.ftp.Port = int.Parse(ntPort.Value + "");
        }

        public string CurrentSrv = "HTTP";

        private void switchToFTPServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentSrv == "HTTP")
            {
                ntMaxClients.Visible = false;
                kryptonLabel2.Visible = false;
                kryptonLabel3.Visible = false;
                lblCurrClt.Visible = false;
                ntPort.Value = Program.ftp.Port;
                txtLog.Clear();

                openPHPiniFileToolStripMenuItem.Visible = false;
                openMYiniFileToolStripMenuItem.Visible = false;
                openWWWRootDirectoryToolStripMenuItem.Visible = false;
                activateTLSToolStripMenuItem.Visible = true;

                showStandardWindowToolStripMenuItem.Enabled = false;
                
                if(Program.ws.Started)
                    Program.ws.Stop();

                //Program.ftp.Start();
                CurrentSrv = "FTP";
                switchToFTPServerToolStripMenuItem.Text = "Switch to HTTP server";
            }
            else
            {
                ntMaxClients.Visible = true;
                kryptonLabel2.Visible = true;
                kryptonLabel3.Visible = true;
                lblCurrClt.Visible = true;
                ntPort.Value = Program.ws.Port;
                txtLog.Clear();

                openPHPiniFileToolStripMenuItem.Visible = true;
                openMYiniFileToolStripMenuItem.Visible = true;
                openWWWRootDirectoryToolStripMenuItem.Visible = true;
                activateTLSToolStripMenuItem.Visible = false;

                showStandardWindowToolStripMenuItem.Enabled = true;

                if(Program.ftp.Started)
                    Program.ftp.Stop();

                //Program.ws.Start();
                CurrentSrv = "HTTP";
                switchToFTPServerToolStripMenuItem.Text = "Switch to FTP server";
            }
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentSrv == "HTTP")
                Program.ws.Start();
            else
                Program.ftp.Start();


            startToolStripMenuItem.Enabled = false;
            stopToolStripMenuItem.Enabled = true;
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CurrentSrv == "FTP")
                Program.ws.Stop();
            else
                Program.ftp.Stop();


            startToolStripMenuItem.Enabled = true;
            stopToolStripMenuItem.Enabled = false;
        }

        private void activateTLSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Program.ftp.TLS)
            {
                Program.ftp.TLS = false;
                activateTLSToolStripMenuItem.Text = "Activate TLS";
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new About().ShowDialog(this);
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new HelpViewer().ShowDialog(this);
        }

        private void chbxHTTPS_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}